/*
 * @(#)DmsContent.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.dms.bean.DmsContent;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;

/**
  DmsContentDAObject.java

  This class is the data access bean for table "DMS_CONTENT".

  @author      Zoe Shum
  @company     DCIVision Limited
  @creation date   31/07/2003
  @version     $Revision: 1.26.2.2 $
*/

public class DmsContentDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.26.2.2 $";

  public static final String TABLE_NAME = "DMS_CONTENT";

  public DmsContentDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  
  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("CONVERTED_NAME");
    this.vecDBColumn.add("CONTENT_TYPE");
    this.vecDBColumn.add("CONTENT");
    this.vecDBColumn.add("EXT");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
    this.vecDBColumn.add("SEGMENT_NO");
  }

  
  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.CONVERTED_NAME, A.CONTENT_TYPE, A.CONTENT, A.EXT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.SEGMENT_NO ");
        sqlStat.append("FROM   DMS_CONTENT A ");
//        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("WHERE  A.ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
//        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          DmsContent tmpDmsContent = new DmsContent();
          tmpDmsContent.setID(getResultSetInteger(rs, "ID"));
          tmpDmsContent.setConvertedName(getResultSetString(rs, "CONVERTED_NAME"));
          tmpDmsContent.setContentType(getResultSetString(rs, "CONTENT_TYPE"));
          
          //tmpDmsContent.setContent(getResultSetObject(rs, "CONTENT"));
          tmpDmsContent.setExt(getResultSetString(rs, "EXT"));
          tmpDmsContent.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsContent.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsContent.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsContent.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsContent.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsContent.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsContent.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsContent.getCreatorID()));
          tmpDmsContent.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsContent.getUpdaterID()));
          tmpDmsContent.setSegmentNO(getResultSetInteger(rs, "SEGMENT_NO"));
          return(tmpDmsContent);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.CONVERTED_NAME, A.CONTENT_TYPE, A.CONTENT, A.EXT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.SEGMENT_NO ");
        sqlStat.append("FROM   DMS_CONTENT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        /*
        if (searchForm.isSearchable()) {
          String searchField = searchForm.getBasicSearchField();
          if (searchField.indexOf(".") < 0) {
            searchField = "A." + searchField;
          }
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }*/
        
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          DmsContent tmpDmsContent = new DmsContent();
          tmpDmsContent.setID(getResultSetInteger(rs, "ID"));
          tmpDmsContent.setConvertedName(getResultSetString(rs, "CONVERTED_NAME"));
          tmpDmsContent.setContentType(getResultSetString(rs, "CONTENT_TYPE"));
          
          //tmpDmsContent.setContent(getResultSetObject(rs, "CONTENT"));
          tmpDmsContent.setExt(getResultSetString(rs, "EXT"));
          tmpDmsContent.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsContent.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsContent.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsContent.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsContent.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsContent.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsContent.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsContent.getCreatorID()));
          tmpDmsContent.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsContent.getUpdaterID()));
          tmpDmsContent.setRecordCount(totalNumOfRecord);
          tmpDmsContent.setRowNum(startOffset++);
          tmpDmsContent.setSegmentNO(getResultSetInteger(rs,"SEGMENT_NO"));
          ++rowLoopCnt;
          result.add(tmpDmsContent);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  
  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.CONVERTED_NAME, A.CONTENT_TYPE, A.CONTENT, A.EXT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.SEGMENT_NO ");
        sqlStat.append("FROM   DMS_CONTENT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsContent tmpDmsContent = new DmsContent();
          tmpDmsContent.setID(getResultSetInteger(rs, "ID"));
          tmpDmsContent.setConvertedName(getResultSetString(rs, "CONVERTED_NAME"));
          tmpDmsContent.setContentType(getResultSetString(rs, "CONTENT_TYPE"));
          
          //tmpDmsContent.setContent(getResultSetObject(rs, "CONTENT"));
          tmpDmsContent.setExt(getResultSetString(rs, "EXT"));
          tmpDmsContent.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsContent.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsContent.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsContent.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsContent.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsContent.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsContent.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsContent.getCreatorID()));
          tmpDmsContent.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsContent.getUpdaterID()));
          tmpDmsContent.setSegmentNO(getResultSetInteger(rs,"SEGMENT_NO"));
          result.add(tmpDmsContent);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsContent tmpDmsContent = (DmsContent)((DmsContent)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        //the convertedName need the contentID converted,so the id is geted first
        if(tmpDmsContent.getID() != null){
          nextID = tmpDmsContent.getID();
        }
        
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   DMS_CONTENT(ID, CONVERTED_NAME, CONTENT_TYPE, CONTENT, EXT, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE, SEGMENT_NO ) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpDmsContent.getConvertedName());
        setPrepareStatement(preStat, 3, tmpDmsContent.getContentType());
        setPrepareStatement(preStat, 4, tmpDmsContent.getContent());
        setPrepareStatement(preStat, 5, tmpDmsContent.getExt());
        setPrepareStatement(preStat, 6, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 7, new Integer(0));
        setPrepareStatement(preStat, 8, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 9, currTime);
        setPrepareStatement(preStat, 10, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 11, currTime);
        setPrepareStatement(preStat,12,tmpDmsContent.getSegmentNO());
        preStat.executeUpdate();
        tmpDmsContent.setID(nextID);
        tmpDmsContent.setCreatorID(sessionContainer.getUserRecordID());
        tmpDmsContent.setCreateDate(currTime);
        tmpDmsContent.setUpdaterID(sessionContainer.getUserRecordID());
        tmpDmsContent.setUpdateDate(currTime);
        tmpDmsContent.setUpdateCount(new Integer(0));
        tmpDmsContent.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsContent.getCreatorID()));
        tmpDmsContent.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsContent.getUpdaterID()));
        return(tmpDmsContent);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsContent tmpDmsContent = (DmsContent)((DmsContent)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE DMS_CONTENT ");
        sqlStat.append("SET  CONVERTED_NAME=?, CONTENT_TYPE=?, CONTENT=?, EXT=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ,SEGMENT_NO=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpDmsContent.getConvertedName());
        setPrepareStatement(preStat, 2, tmpDmsContent.getContentType());
        setPrepareStatement(preStat, 3, tmpDmsContent.getContent());
        setPrepareStatement(preStat, 4, tmpDmsContent.getExt());
        setPrepareStatement(preStat, 5, new Integer(tmpDmsContent.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 6, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 7, currTime);
        setPrepareStatement(preStat, 8,tmpDmsContent.getSegmentNO());
        setPrepareStatement(preStat, 9, tmpDmsContent.getID());
        setPrepareStatement(preStat, 10, tmpDmsContent.getUpdateCount());
        
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpDmsContent.setUpdaterID(sessionContainer.getUserRecordID());
          tmpDmsContent.setUpdateDate(currTime);
          tmpDmsContent.setUpdateCount(new Integer(tmpDmsContent.getUpdateCount().intValue() + 1));
          tmpDmsContent.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsContent.getCreatorID()));
          tmpDmsContent.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsContent.getUpdaterID()));
          return(tmpDmsContent);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsContent tmpDmsContent = (DmsContent)((DmsContent)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   DMS_CONTENT ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpDmsContent.getID());
        setPrepareStatement(preStat, 2, tmpDmsContent.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpDmsContent);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    DmsContent tmpDmsContent = (DmsContent)this.oldValue;

    if (tmpDmsContent != null) {
      oldValues.add(toAuditTrailValue(tmpDmsContent.getID()));
      oldValues.add(toAuditTrailValue(tmpDmsContent.getConvertedName()));
      oldValues.add(toAuditTrailValue(tmpDmsContent.getContentType()));
      oldValues.add(toAuditTrailValue(tmpDmsContent.getContent()));
      oldValues.add(toAuditTrailValue(tmpDmsContent.getExt()));
      oldValues.add(toAuditTrailValue(tmpDmsContent.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpDmsContent.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpDmsContent.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpDmsContent.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpDmsContent.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpDmsContent.getUpdateDate()));
    }

    tmpDmsContent = (DmsContent)obj;
    if (tmpDmsContent != null) {
      newValues.add(toAuditTrailValue(tmpDmsContent.getID()));
      newValues.add(toAuditTrailValue(tmpDmsContent.getConvertedName()));
      newValues.add(toAuditTrailValue(tmpDmsContent.getContentType()));
      newValues.add(toAuditTrailValue(tmpDmsContent.getContent()));
      newValues.add(toAuditTrailValue(tmpDmsContent.getExt()));
      newValues.add(toAuditTrailValue(tmpDmsContent.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpDmsContent.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpDmsContent.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpDmsContent.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpDmsContent.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpDmsContent.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/

  /**
   * Gets list of the DmsContent object by list of DmsContent ID.
   * 
   * @param idList list of DmsContent object ID
   * @return List of DmsContent
   * @throws ApplicationException
   */
  public synchronized List getListByIDList(List idList) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.CONVERTED_NAME, A.CONTENT_TYPE, A.CONTENT, A.EXT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.SEGMENT_NO ");
        sqlStat.append("FROM   DMS_CONTENT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND ( ");
        for (int i=0; i<idList.size(); i++) {
          sqlStat.append("A.ID = ? OR ");
        }
        sqlStat.append(" ) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        for (int i=0; i<idList.size(); i++) {
           this.setPrepareStatement(preStat, i+2, idList.get(i));
        }
        //log.debug("sql = " +sqlStat.toString());
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsContent tmpDmsContent = new DmsContent();
          tmpDmsContent.setID(getResultSetInteger(rs, "ID"));
          tmpDmsContent.setConvertedName(getResultSetString(rs, "CONVERTED_NAME"));
          tmpDmsContent.setContentType(getResultSetString(rs, "CONTENT_TYPE"));
          
          //tmpDmsContent.setContent(getResultSetObject(rs, "CONTENT"));
          tmpDmsContent.setExt(getResultSetString(rs, "EXT"));
          tmpDmsContent.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsContent.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsContent.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsContent.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsContent.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsContent.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsContent.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsContent.getCreatorID()));
          tmpDmsContent.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsContent.getUpdaterID()));
          tmpDmsContent.setSegmentNO(getResultSetInteger(rs,"SEGMENT_NO"));
          result.add(tmpDmsContent);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Deletes the record of table "DmsContent" by ID.
   * 
   * @param contentID the DmsContent object ID
   * @throws ApplicationException
   */
  public synchronized void hardDeleteByID(Integer contentID) throws ApplicationException {
    PreparedStatement preStat = null;
	    StringBuffer sqlStat = new StringBuffer();
	

	    synchronized(dbConn) {
	      try {
	        int updateCnt = 0;
		        sqlStat.append("DELETE ");
		        sqlStat.append("FROM   DMS_CONTENT ");
		        sqlStat.append("WHERE  ID=?  ");		preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		        setPrepareStatement(preStat, 1, contentID);

		        updateCnt = preStat.executeUpdate();
	      }catch (SQLException sqle) {
		        log.error(sqle, sqle);
		        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	      } catch (Exception e) {
		        log.error(e, e);
		        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
	      } finally {
	        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
	      }
	    }
 }
 
 /**
  * Gets list of the DmsContent objects by converted name and location master id
  * 
  * @param convertedName the encryted document name
  * @param locMasterID the location master ID 
  * @return list of DmsContent object
  * @throws ApplicationException
  * @see com.dcivision.dms.bean.DmsLocMaster
  * @see com.dcivision.dms.bean.DmsContent
  */
 public synchronized List getListByConvertedNameUnderSameLoc(String convertedName, Integer locMasterID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.CONVERTED_NAME, A.CONTENT_TYPE, A.CONTENT, A.EXT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.SEGMENT_NO ");
        sqlStat.append("FROM   DMS_CONTENT A, DMS_VERSION V, DMS_DOCUMENT D, DMS_ROOT R ");
        sqlStat.append("WHERE A.CONVERTED_NAME = ? ");
        sqlStat.append("AND R.LOC_ID = ? ");
        sqlStat.append("AND A.ID = V.CONTENT_ID ");
        sqlStat.append("AND V.DOCUMENT_ID = D.ID ");
        sqlStat.append("AND D.ROOT_ID=R.ID ");
        
        
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        
        this.setPrepareStatement(preStat, 1, convertedName);
        this.setPrepareStatement(preStat, 2, locMasterID);
        //log.debug("sql = " +sqlStat.toString());
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsContent tmpDmsContent = new DmsContent();
          tmpDmsContent.setID(getResultSetInteger(rs, "ID"));
          tmpDmsContent.setConvertedName(getResultSetString(rs, "CONVERTED_NAME"));
          tmpDmsContent.setContentType(getResultSetString(rs, "CONTENT_TYPE"));
          
          //tmpDmsContent.setContent(getResultSetObject(rs, "CONTENT"));
          tmpDmsContent.setExt(getResultSetString(rs, "EXT"));
          tmpDmsContent.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsContent.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsContent.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsContent.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsContent.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsContent.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsContent.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsContent.getCreatorID()));
          tmpDmsContent.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsContent.getUpdaterID()));
          tmpDmsContent.setSegmentNO(getResultSetInteger(rs,"SEGMENT_NO"));
          result.add(tmpDmsContent);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
 
 public synchronized AbstractBaseObject getByContentName(String contentName) throws ApplicationException {
   PreparedStatement preStat = null;
   ResultSet rs = null;
   StringBuffer sqlStat = new StringBuffer();

   synchronized(dbConn) {
     try {
       sqlStat.append("SELECT A.ID, A.CONVERTED_NAME, A.CONTENT_TYPE, A.CONTENT, A.EXT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.SEGMENT_NO ");
       sqlStat.append("FROM   DMS_CONTENT A ");
//       sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
       sqlStat.append("WHERE  A.CONVERTED_NAME = ? ");
       preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       this.setPrepareStatement(preStat, 1, contentName);
//       this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
       rs = preStat.executeQuery();
       DmsContent tmpDmsContent =null;
       if (rs.next()) {
         tmpDmsContent = new DmsContent();
         tmpDmsContent.setID(getResultSetInteger(rs, "ID"));
         tmpDmsContent.setConvertedName(getResultSetString(rs, "CONVERTED_NAME"));
         tmpDmsContent.setContentType(getResultSetString(rs, "CONTENT_TYPE"));
         
         //tmpDmsContent.setContent(getResultSetObject(rs, "CONTENT"));
         tmpDmsContent.setExt(getResultSetString(rs, "EXT"));
         tmpDmsContent.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
         tmpDmsContent.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
         tmpDmsContent.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
         tmpDmsContent.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
         tmpDmsContent.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
         tmpDmsContent.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
         tmpDmsContent.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsContent.getCreatorID()));
         tmpDmsContent.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsContent.getUpdaterID()));
         tmpDmsContent.setSegmentNO(getResultSetInteger(rs, "SEGMENT_NO"));
       }
       return(tmpDmsContent);
     } catch (ApplicationException appEx) {
       throw appEx;
     } catch (SQLException sqle) {
       log.error(sqle, sqle);
       throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
     } catch (Exception e) {
       log.error(e, e);
       throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
     } finally {
       try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
       try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
     }
   }
 }
 

 public synchronized List getContentListByLocalMasterID(Integer localMasterID) throws ApplicationException {
   PreparedStatement preStat = null;
   ResultSet rs = null;
   StringBuffer sqlStat = new StringBuffer();
   List result = new ArrayList();

   synchronized(dbConn) {
     try {
       sqlStat.append("SELECT C.ID,C.CONVERTED_NAME,C.CONTENT_TYPE,C.EXT,C.RECORD_STATUS,C.UPDATE_COUNT,C.CREATOR_ID,C.CREATE_DATE,C.UPDATER_ID,C.UPDATE_DATE,C.SEGMENT_NO FROM DMS_DOCUMENT A,DMS_VERSION B,DMS_CONTENT C,DMS_ROOT D , DMS_LOC_MASTER E WHERE ");
       sqlStat.append("  A.ROOT_ID = D.ID AND B.DOCUMENT_ID = A.ID AND C.ID = B.CONTENT_ID ");       
       sqlStat.append(" AND D.LOC_ID = E.ID AND E.ID=? ORDER BY C.ID ");
       preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       this.setPrepareStatement(preStat, 1, localMasterID);
       rs = preStat.executeQuery();
       while (rs.next()) {
         DmsContent tmpDmsContent = new DmsContent();
         tmpDmsContent.setID(getResultSetInteger(rs, "ID"));
         tmpDmsContent.setConvertedName(getResultSetString(rs, "CONVERTED_NAME"));
         tmpDmsContent.setContentType(getResultSetString(rs, "CONTENT_TYPE"));
         
         //tmpDmsContent.setContent(getResultSetObject(rs, "CONTENT"));
         tmpDmsContent.setExt(getResultSetString(rs, "EXT"));
         tmpDmsContent.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
         tmpDmsContent.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
         tmpDmsContent.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
         tmpDmsContent.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
         tmpDmsContent.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
         tmpDmsContent.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
         tmpDmsContent.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsContent.getCreatorID()));
         tmpDmsContent.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsContent.getUpdaterID()));
         tmpDmsContent.setSegmentNO(getResultSetInteger(rs,"SEGMENT_NO"));
         result.add(tmpDmsContent);
       }
       return(result);
     } catch (ApplicationException appEx) {
       throw appEx;
     } catch (SQLException sqle) {
       log.error(sqle, sqle);
       throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
     } catch (Exception e) {
       log.error(e, e);
       throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
     } finally {
       try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
       try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
     }
   }
 }
 
 public synchronized AbstractBaseObject insertOnly(AbstractBaseObject obj) throws ApplicationException {
   return this.insert(obj);
 }
 
 public synchronized AbstractBaseObject updateOnly(AbstractBaseObject obj) throws ApplicationException {
   return this.update(obj);
 }
 
 public synchronized void updateByFields(
     String[] fields, 
     Object[] values,
     Integer id,
     Integer updateCount) throws ApplicationException {
   PreparedStatement preStat = null;
   StringBuffer sqlStat = new StringBuffer();

   synchronized(dbConn) {
     try {
       int updateCnt = 0;
       Timestamp currTime = Utility.getCurrentTimestamp();
       sqlStat.append("UPDATE DMS_CONTENT ");
       sqlStat.append("SET UPDATE_COUNT=UPDATE_COUNT+1, UPDATER_ID=?, UPDATE_DATE=? ");
       if (fields!=null) {
         for (int i=0; i<fields.length; i++) {
           sqlStat.append("," +fields[i]+ "=?  ");
         }
       }
       sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
       preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       setPrepareStatement(preStat, 1, sessionContainer.getUserRecordID());
       setPrepareStatement(preStat, 2, currTime);
       int counter = 3;
       if (fields!=null) {
         for (int i=0; i<values.length; i++) {
           if (values[i]==null) {
             preStat.setObject(counter++, null);
           }else {
             String className = values[i].getClass().getName();
             if (className.indexOf("Integer")!=-1) {
               setPrepareStatement(preStat, counter++, (Integer)values[i]);
             }else if (className.indexOf("String")!=-1) {
               setPrepareStatement(preStat, counter++, (String)values[i]);
             }else if (className.indexOf("Timestamp")!=-1) {
               setPrepareStatement(preStat, counter++, (Timestamp)values[i]);
             }else {
               throw new ApplicationException("DmsContentDAObject.updateFields() does not support this field type:"+className);
             }
           }
         }
       }
       setPrepareStatement(preStat, counter++, id);
       setPrepareStatement(preStat, counter++, updateCount);
       updateCnt = preStat.executeUpdate();
       if (updateCnt == 0) {
         throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
       }
     } catch (ApplicationException appEx) {
       throw appEx;
     } catch (SQLException sqle) {
       log.error(sqle, sqle);
       throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
     } catch (Exception e) {
       log.error(e, e);
       throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
     } finally {
       try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
     }
   }
 }
 
 public synchronized List getContentListByVersionIDList(List versionIds) throws ApplicationException {
   PreparedStatement preStat = null;
   ResultSet rs = null;
   StringBuffer sqlStat = new StringBuffer();
   synchronized(dbConn) {
     try {
       sqlStat.append("SELECT A.ID, A.CONVERTED_NAME, A.CONTENT_TYPE, A.CONTENT, A.EXT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.SEGMENT_NO ");
       sqlStat.append("FROM   DMS_CONTENT A INNER JOIN DMS_VERSION B ON A.ID=B.CONTENT_ID ");
       sqlStat.append("WHERE  B.ID IN (");
       for (int i=0; i<versionIds.size(); i++) {
         sqlStat.append(i!=0?",?":"?");
       }
       sqlStat.append(") ");
       sqlStat.append("ORDER BY B.ID");
       
       preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       int counter = 1;
       for (int i=0; i<versionIds.size(); i++) {
         this.setPrepareStatement(preStat, counter++, (Integer)versionIds.get(i));
       }
       rs = preStat.executeQuery();

       List result = new ArrayList();
       while (rs.next()) {
         DmsContent tmpDmsContent = new DmsContent();
         tmpDmsContent.setID(getResultSetInteger(rs, "ID"));
         tmpDmsContent.setConvertedName(getResultSetString(rs, "CONVERTED_NAME"));
         tmpDmsContent.setContentType(getResultSetString(rs, "CONTENT_TYPE"));
         
         //tmpDmsContent.setContent(getResultSetObject(rs, "CONTENT"));
         tmpDmsContent.setExt(getResultSetString(rs, "EXT"));
         tmpDmsContent.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
         tmpDmsContent.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
         tmpDmsContent.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
         tmpDmsContent.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
         tmpDmsContent.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
         tmpDmsContent.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
         tmpDmsContent.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsContent.getCreatorID()));
         tmpDmsContent.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsContent.getUpdaterID()));
         tmpDmsContent.setSegmentNO(getResultSetInteger(rs, "SEGMENT_NO"));
         result.add(tmpDmsContent);
       }
       return(result);
     } catch (ApplicationException appEx) {
       throw appEx;
     } catch (SQLException sqle) {
       log.error(sqle, sqle);
       throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
     } catch (Exception e) {
       log.error(e, e);
       throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
     } finally {
       try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
       try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
     }
   }
 }
 
 public synchronized List getContentListByDocumentIDList(List documentIds) throws ApplicationException {
   PreparedStatement preStat = null;
   ResultSet rs = null;
   StringBuffer sqlStat = new StringBuffer();
   synchronized(dbConn) {
     try {
       sqlStat.append("SELECT A.ID, A.CONVERTED_NAME, A.CONTENT_TYPE, A.CONTENT, A.EXT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.SEGMENT_NO ");
       sqlStat.append("FROM   DMS_CONTENT A INNER JOIN DMS_VERSION B ON A.ID=B.CONTENT_ID ");
       sqlStat.append("WHERE  B.DOCUMENT_ID IN (");
       for (int i=0; i<documentIds.size(); i++) {
         sqlStat.append(i!=0?",?":"?");
       }
       sqlStat.append(") ");
       sqlStat.append("ORDER BY B.DOCUMENT_ID");
       
       preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       int counter = 1;
       for (int i=0; i<documentIds.size(); i++) {
         this.setPrepareStatement(preStat, counter++, (Integer)documentIds.get(i));
       }
       rs = preStat.executeQuery();

       List result = new ArrayList();
       while (rs.next()) {
         DmsContent tmpDmsContent = new DmsContent();
         tmpDmsContent.setID(getResultSetInteger(rs, "ID"));
         tmpDmsContent.setConvertedName(getResultSetString(rs, "CONVERTED_NAME"));
         tmpDmsContent.setContentType(getResultSetString(rs, "CONTENT_TYPE"));
         
         //tmpDmsContent.setContent(getResultSetObject(rs, "CONTENT"));
         tmpDmsContent.setExt(getResultSetString(rs, "EXT"));
         tmpDmsContent.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
         tmpDmsContent.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
         tmpDmsContent.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
         tmpDmsContent.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
         tmpDmsContent.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
         tmpDmsContent.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
         tmpDmsContent.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsContent.getCreatorID()));
         tmpDmsContent.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsContent.getUpdaterID()));
         tmpDmsContent.setSegmentNO(getResultSetInteger(rs, "SEGMENT_NO"));
         result.add(tmpDmsContent);
       }
       return(result);
     } catch (ApplicationException appEx) {
       throw appEx;
     } catch (SQLException sqle) {
       log.error(sqle, sqle);
       throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
     } catch (Exception e) {
       log.error(e, e);
       throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
     } finally {
       try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
       try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
     }
   }
 }
 
 /**
  * Gets list of the DmsContent objects by converted name and location master id
  * 
  * @param convertedName the encryted document name
  * @param locMasterID the location master ID 
  * @return list of DmsContent object
  * @throws ApplicationException
  * @see com.dcivision.dms.bean.DmsLocMaster
  * @see com.dcivision.dms.bean.DmsContent
  */
 public synchronized boolean checkHasListByConvertedNameUnderSameLoc(String convertedName, Integer locMasterID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStat1 = new StringBuffer();
    boolean hasSameConvertedNameList = false;
    boolean isShare = false;
    synchronized(dbConn) {
      try {
        //1.check if DMS_CONTENT have record converted_name is convertedName        
        
        sqlStat.append("SELECT count(*)");
        sqlStat.append("FROM   DMS_CONTENT A ");
        sqlStat.append("WHERE A.CONVERTED_NAME = ? ");
        
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, convertedName);
        //log.debug("sql = " +sqlStat.toString());
        log.debug("sql = "+sqlStat.toString());
        rs = preStat.executeQuery();
        if (rs.next()) {
          if(rs.getInt(1)>1){
            hasSameConvertedNameList = true;
          }
        }

        //2. if DMS_CONTENT have record converted_name is convertedName record then to check the rootid if differ
        if(hasSameConvertedNameList){
          //sqlStat.delete(0,sqlStat.length());
          sqlStat1.append("SELECT count(*)");
          sqlStat1.append("FROM   DMS_CONTENT A, DMS_VERSION V, DMS_DOCUMENT D, DMS_ROOT R ");
          sqlStat1.append("WHERE A.CONVERTED_NAME = ? ");
          sqlStat1.append("AND A.ID = V.CONTENT_ID ");
          sqlStat1.append("AND V.DOCUMENT_ID = D.ID ");
          sqlStat1.append("AND D.ROOT_ID=R.ID ");        
          sqlStat1.append("AND R.LOC_ID = ? ");
          
          preStat = dbConn.prepareStatement(sqlStat1.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          
          this.setPrepareStatement(preStat, 1, convertedName);
          this.setPrepareStatement(preStat, 2, locMasterID);
          //log.debug("sql = " +sqlStat.toString());
          log.debug("sql = "+sqlStat1.toString());
          rs = preStat.executeQuery();
          if (rs.next()) {
            if(rs.getInt(1)>0){
              isShare = true;
            }
          }        
        }
        return isShare;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
 
 
}
